using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class CreateAbilityWindow : MonoBehaviour {
	
	public GameObject BrowseSelectSearchWindowPrefab;
	
	public UICheckbox iterative;
	public UICheckbox replacementAbility;
	public UILabel Name;
	public UILabel Description;
	public UILabel placeHolderListLabel;
	
	public GameObject OKButton;
	public GameObject CancelButton;
	public GameObject DefineReplacementsButton;
	
	public GameObject AddModifierButton;
	List<string> buffs;
	List<GameObject> buffObjects;
	
	public List<string> replacements;
	
	public GameObject target;
	public string functionName;
	
	public UITable holder;
	
	string origName;
	Ability a;
	
	bool loaded = false;
	
	public bool isFeat = false;

	// Use this for initialization
	void Start () {
		UIButtonMessage message = replacementAbility.gameObject.AddComponent<UIButtonMessage>();
		message.target = gameObject;
		message.functionName = "RepositionTable";
		
		message = AddModifierButton.AddComponent<UIButtonMessage>();
		message.target = gameObject;
		message.functionName = "AddModifierClicked";
		
		if(!loaded)
		{
			buffs = new List<string>();
			buffObjects = new List<GameObject>();
			
			message = OKButton.AddComponent<UIButtonMessage>();
			message.target = gameObject;
			message.functionName = "OKClicked";
			
			message = CancelButton.AddComponent<UIButtonMessage>();
			message.target = gameObject;
			message.functionName = "CancelClicked";
			
			message = DefineReplacementsButton.AddComponent<UIButtonMessage>();
			message.target = gameObject;
			message.functionName = "AddReplacementAbilities";
		}
		
		RepositionTable();
	}
	
	void RepositionTable()
	{
		holder.repositionNow = true;
	}
	
	public void LoadAbilityDisplay(string ability, bool Feat, bool noEdit = false)
	{
		//Debug.Log(Feat);
		buffs = new List<string>();
		buffObjects = new List<GameObject>();
		
		
		if(Feat)
		{
			a = FeatManager.Load(ability);
		}
		else
		{
			a = SpecialAbilityManager.Load(ability);
		}
		
		//Debug.Log(a);
		isFeat = Feat;
		
		iterative.isChecked = a.iterative;
		replacementAbility.isChecked = a.replacementAbility;
		Description.text = a.description;
		Name.text = a.name;
		origName = ability;
		
		if(a.iterative)
		{
			Name.text = a.iterativeName;
		}
		
		if(a.replacementAbility)
		{
			replacements = a.replacementList;
			
			string s = "";
			foreach(string r in replacements)
			{
				if(s != "")
					s += ", ";
				s += r;
			}
			
			placeHolderListLabel.text = s;
		}
		
		loaded = true;
		
		bool isCustom;
		if(isFeat)
			isCustom = FeatManager.isCustom(a.name);
		else
			isCustom = SpecialAbilityManager.isCustom(a.name);
		
		CreateBuffObjects(a.buffs.ConvertAll<string>(e => e.name));
		
		#warning dont allow edit of non-custom on final build
		if(noEdit)//!isCustom && Application.platform != RuntimePlatform.WindowsEditor)
		{
			NGUITools.AddWidgetCollider(gameObject);
			
			foreach(Collider l in gameObject.GetComponentsInChildren<Collider>())
			{
				if(l.gameObject.name != "Content Panel")
					l.enabled = false;
			}
			
			collider.enabled = true;
			
			DefineReplacementsButton.SetActiveRecursively(false);
			CancelButton.SetActiveRecursively(false);
			
			UIButtonMessage message = OKButton.AddComponent<UIButtonMessage>();		
			message.target = gameObject;
			message.functionName = "CancelClicked";
			message.collider.enabled = true;
		}
		else
		{
			foreach(UIInput l in gameObject.GetComponentsInChildren<UIInput>())
			{
				l.text = l.label.text;
				l.mDefaultText = "Ability Info";
			}
			
			UIButtonMessage message = OKButton.AddComponent<UIButtonMessage>();		
			message.target = gameObject;
			message.functionName = "SaveChangesClicked";
			
			CancelButton.GetComponentInChildren<UILabel>().text = "Delete!";
			message = CancelButton.AddComponent<UIButtonMessage>();		
			message.target = gameObject;
			message.functionName = "DeleteClicked";
			
			message = DefineReplacementsButton.AddComponent<UIButtonMessage>();
			message.target = gameObject;
			message.functionName = "AddReplacementAbilities";
		}
	}
	
	void AddModifierClicked()
	{
		BrowserManager.CreateBuffsBrowser(gameObject, "CreateBuffObjects", buffs, false);
	}
	
	void CreateBuffObjects(List<string> selections)
	{
		if(selections != null)
			buffs = selections;
		
		foreach(GameObject buff in buffObjects)
		{
			Destroy(buff);
		}
		
		buffs.Sort();
		for(int i = 0; i < buffs.Count; i++)
		{
			GameObject g = Instantiate(GameReference.reference.SkillLabelPrefab) as GameObject;
			g.name = "g " + (i+2) + " BuffLabel";
			g.transform.parent = AddModifierButton.transform.parent.parent;
			g.active = true;
			g.transform.localScale = Vector3.one;
			g.transform.position = AddModifierButton.transform.position;
			
			BuffDebuff b = BuffDebuffManager.Load(buffs[i]);
			
			for(int ii = 0; ii < b.modifiers.Count; ii++)
			{
				TemporaryBonus tb = b.modifiers[ii];
				UILabel bonusLabel = NGUITools.AddWidget<UILabel>(g);
				bonusLabel.transform.localScale = Vector3.one * 18;
				bonusLabel.transform.localPosition = Vector3.down * 20 * ii + Vector3.down * 20 + Vector3.left * 120;
				bonusLabel.text = tb.GetDescription();
				bonusLabel.lineWidth = 185;
				bonusLabel.multiLine = true;
				bonusLabel.pivot = UIWidget.Pivot.Left;
			}
			
			g.GetComponentInChildren<UIDragObject>().target = holder.transform;
			
			g.GetComponentInChildren<UILabel>().text = buffs[i];
			buffObjects.Add(g);
		}
		
		holder.repositionNow = true;
	}
	
	void OKClicked()
	{
		Ability a;
		
		if(isFeat)
			a = FeatManager.AddFeat(Name.text, Description.text, iterative.isChecked, replacementAbility.isChecked, replacements, buffs);
		else
			a = SpecialAbilityManager.AddSpecialAbility(Name.text, Description.text, iterative.isChecked, replacementAbility.isChecked, replacements, buffs);
		
		if(target != null)
			target.SendMessage(functionName, new KeyValuePair<string,string>(a.name, Description.text), SendMessageOptions.DontRequireReceiver);
	
		Destroy(gameObject);
	}
	
	void SaveChangesClicked()
	{
		a.name = Name.text;
		a.description = Description.text;
		a.iterative = iterative.isChecked;
		a.replacementAbility = replacementAbility.isChecked;
		a.replacementList = replacements;
		a.buffs = buffs.ConvertAll<BuffDebuff>(bs => BuffDebuffManager.Load(bs));
		
		bool savedThing;
		if(isFeat)
			savedThing = FeatManager.SaveAbility(a, origName);
		else
			savedThing = SpecialAbilityManager.SaveAbility(a, origName);
		
		if(savedThing)
		{
			target.SendMessage("renamedObject", new KeyValuePair<string, string>(origName, a.name), SendMessageOptions.DontRequireReceiver);
			
			Destroy(gameObject);
		}
	}
	
	void CancelClicked()
	{
		Destroy(gameObject);
	}
	
	IEnumerator DeleteClicked()
	{
		List<bool> b = new List<bool>{false};
		
		yield return StartCoroutine(HelperFunctions.ConfirmationWindow("Are you sure you want to delete " + origName + "?", b));
		
		if(b[0])
		{
			if(isFeat)
				FeatManager.DeleteAbility(origName);
			else
				SpecialAbilityManager.DeleteAbility(origName);
			
			if(target != null)
				target.SendMessage(functionName);
		}

		Destroy(gameObject);
	}
	
	void AddReplacementAbilities()
	{
		BrowserManager.CreateAbilityBrowser(gameObject, "UpdateReplacementAbilities", replacements, true, true, true);
	}
	
	void UpdateReplacementAbilities(List<string> selections)
	{
		replacements = selections;
			
		string s = "";
		foreach(string r in replacements)
		{
			if(s != "")
				s += ", ";
			s += r;
		}
		
		placeHolderListLabel.text = s;
		
		holder.repositionNow = true;
	}
}
